package com.iims.finance.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iims.common.exception.CustomException;
import com.iims.common.utils.SeqNumUtils;
import com.iims.finance.domain.CmisCwInvoice;
import com.iims.finance.enums.ReviewStatus;
import com.iims.finance.mapper.CmisCwInvoiceMapper;
import com.iims.finance.service.ICmisCwInvoiceService;
import com.iims.finance.utils.FinanceBusinessKeyUtils;
import com.iims.finance.vo.resp.CmisCwInvoiceDetailVo;
import com.iims.flowable.service.IFlowInstanceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author fanpei
 * @date 2021/7/9 22:31
 */
@Service
public class CmisCwInvoiceServiceImpl extends ServiceImpl<CmisCwInvoiceMapper, CmisCwInvoice> implements ICmisCwInvoiceService {

    @Autowired
    private IFlowInstanceService instanceService;

    @Override
    public List<CmisCwInvoiceDetailVo> getCwInvoiceList(CmisCwInvoice invoice) {
        List list =  baseMapper.selectCwInvoiceList(invoice);
        return SeqNumUtils.initSeqNum(invoice, list);
    }

    @Override
    public List<CmisCwInvoiceDetailVo> getContractCwInvoiceList(Long contractId) {
        return baseMapper.selectContractCwInvoiceList(contractId);
    }

    @Override
    public CmisCwInvoiceDetailVo getCwInvoiceDetailById(Long proId) {
        return baseMapper.selectCwInvoiceDetail(proId);
    }

    @Override
    public void insertCwInvoice(CmisCwInvoice invoice) {
        baseMapper.insert(invoice);
    }

    @Override
    public void updateCwInvoice(CmisCwInvoice invoice) {
        LambdaUpdateWrapper<CmisCwInvoice> updateWrapper = new LambdaUpdateWrapper<CmisCwInvoice>()
                .eq(CmisCwInvoice::getInvoiceId, invoice.getInvoiceId())
                .set(CmisCwInvoice::getInvoiceCode, invoice.getInvoiceCode())
                .set(CmisCwInvoice::getInvoiceNo, invoice.getInvoiceNo())
                .set(CmisCwInvoice::getContractId, invoice.getContractId())
                .set(CmisCwInvoice::getInvoiceMoney, invoice.getInvoiceMoney())
                .set(CmisCwInvoice::getInvoiceDate, invoice.getInvoiceDate())
                .set(CmisCwInvoice::getInvoiceUser, invoice.getInvoiceUser())
                .set(CmisCwInvoice::getPoster, invoice.getPoster())
                .set(CmisCwInvoice::getCollector, invoice.getCollector())
                .set(CmisCwInvoice::getRemark, invoice.getRemark());
        baseMapper.update(null, updateWrapper);
    }

    @Override
    public void deleteById(Long invoiceId) {
        checkCmisCwInvoiceUse(invoiceId);
        baseMapper.deleteById(invoiceId);
    }

    @Override
    public void checkCmisCwInvoiceUse(Long invoiceId) {

        CmisCwInvoice invoice = baseMapper.selectOne(new LambdaQueryWrapper<CmisCwInvoice>()
                .select(CmisCwInvoice::getInvoiceId, CmisCwInvoice::getReviewStatus).eq(CmisCwInvoice::getInvoiceId, invoiceId));

        if (ReviewStatus.HasReviewed.getValue() == invoice.getReviewStatus().intValue()) {
            throw new CustomException("已审核的开票记录禁止删除!");
        }
        String businessKey = FinanceBusinessKeyUtils.getInvoiceBusinessKey(invoice);
        if (instanceService.checkBusinessInFlow(businessKey)) {
            throw new CustomException("开票已在流程中运转,无法删除!");
        }
    }
}
